﻿using MvcFrameStruct.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcFrameStruct.Areas.User.Controllers
{
    public class IndexController : Controller
    {
        TestEntities testentity = new TestEntities();
        //
        // GET: /User/Index/

        public ActionResult Index()
        {
            List<Models.User> list = new List<Models.User>();
            foreach (var item in testentity.base_user.ToList())
            {
                Models.User user = new Models.User();
                user.Name = item.usname;
                user.UserId = item.usid;
                user.UserSex = (bool)item.ussex;
                user.BirthDate = (DateTime)item.borthdate;

                list.Add(user);
            }
            return View(list);
        }

        public ActionResult AddUser()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddUser(User.Models.User user)
        {
            if (ModelState.IsValid) //通过验证
            {
                base_user userobj = new base_user();
                userobj.borthdate = user.BirthDate;
                userobj.usname = user.Name;
                userobj.ussex = user.UserSex;

                testentity.base_user.Add(userobj);
                testentity.SaveChanges();

                return RedirectToAction("Index");
            }
            else {
                return View();
            }
        }

        public ActionResult Show(int id, string flag)
        {
            base_user obj = testentity.base_user.Where(o => o.usid == id).FirstOrDefault();

            if (flag == "Delete")
            {
                return View("~/Areas/User/Views/Index/Delete.cshtml", obj);
            }
            else
            {
                Models.User userobj = new Models.User();
                userobj.BirthDate = (DateTime)obj.borthdate;
                userobj.Name = obj.usname;
                userobj.UserId = obj.usid;
                userobj.UserSex = (bool)obj.ussex;
                return View("~/Areas/User/Views/Index/Update.cshtml", userobj);
            }
        }

        public ActionResult Delete(string usid)
        {
            int uid = int.Parse(usid);
            base_user obj = testentity.base_user.Where(o => o.usid == uid).FirstOrDefault();
            testentity.base_user.Remove(obj);
            testentity.SaveChanges();
            return RedirectToAction("Index");
        } 

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="usid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Update(User.Models.User userobj, string usid)
        {
            if (ModelState.IsValid)
            {
                int uid = int.Parse(usid);
                base_user obj = testentity.base_user.Where(o => o.usid == uid).FirstOrDefault();
                obj.borthdate = userobj.BirthDate;
                obj.usname = userobj.Name;
                obj.ussex = userobj.UserSex;
                testentity.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }
    }
}
